//
//  YCCoreDataManager+CDProduct.m
//  DemoCoreData
//
//  Created by tangkexiong on 16/3/16.
//  Copyright © 2016年 tangkexiong. All rights reserved.
//

#import "YCCoreDataManager+CDProduct.h"

#import "CDProduct.h"
#import "CDProductCategory.h"

#define kEntiyProduct @"CDProduct"
#define kEntiyProductCategory @"CDProductCategory"

@implementation YCCoreDataManager (CDProduct)

- (void)insertProduct:(YCProduct *)product
                block:(void (^)(NSError *,
                                YCProduct *))block {
    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
        NSManagedObjectContext *context = [self managedObjectContext];
        
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kEntiyProductCategory];
        request.predicate = [NSPredicate predicateWithFormat:@"mId=%@", product.mCategory.mId];
        NSArray *result = [context executeFetchRequest:request error:nil];
        CDProductCategory *cdProductCategory = result.count > 0 ? result[0] : nil;
        
        CDProduct *cdProduct = [NSEntityDescription insertNewObjectForEntityForName:kEntiyProduct inManagedObjectContext:context];
        cdProduct.mName = product.mName;
        cdProduct.mPrice = product.mPrice;
        cdProduct.mId = @([[NSDate date] timeIntervalSince1970]);
        cdProduct.mCategory = cdProductCategory;
        
        NSError *error = nil;
        [context save:&error];
        
        product.mId = cdProduct.mId;
        
        block(error, product);
    });
}

- (void)deleteProduct:(YCProduct *)product
                block:(void (^)(NSError *))block {
    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
        NSManagedObjectContext *context = self.managedObjectContext;
        
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kEntiyProduct];
        
        request.predicate = [NSPredicate predicateWithFormat:@"mId=%@", product.mId];
        
        NSArray *result = [context executeFetchRequest:request error:nil];
        
        for (CDProduct *temp in result) {
            [context deleteObject:temp];
        }
        
        NSError *error = nil;
        [context save:&error];
        
        block(error);
    });
}

- (void)updateProduct:(YCProduct *)product block:(void (^)(NSError *, YCProduct *))block {
    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
        NSManagedObjectContext *context = self.managedObjectContext;
        
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kEntiyProductCategory];
        request.predicate = [NSPredicate predicateWithFormat:@"mId=%@", product.mCategory.mId];
        NSArray *result = [context executeFetchRequest:request error:nil];
        CDProductCategory *cdProductCategory = result.count > 0 ? result[0] : nil;
        
        {
            NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:kEntiyProduct];
            request.predicate = [NSPredicate predicateWithFormat:@"mId=%@", product.mId];
            NSArray *result = [context executeFetchRequest:request error:nil];
            
            for (CDProduct *temp in result) {
                temp.mName = product.mName;
                temp.mPrice = product.mPrice;
                temp.mCategory = cdProductCategory;
            }
        }
        
        NSError *error = nil;
        [context save:&error];
        
        block(error, product);
    });
}

@end
